/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.thrift.naming;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;

/**
 * Provides FQN for thrift files. The FQN of a thrift element consist of the full name in the tree path plus
 * the name of the thrift file.
 * 
 * @author Thomas Kistel
 * @version $Revision: 759 $
 */
public class ThriftQualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider {

    public QualifiedName getFullyQualifiedName(final EObject obj) {
        QualifiedName qualifiedName = super.getFullyQualifiedName(obj);
        return addFileName(obj, qualifiedName);
    }

    /**
     * Adds the filename of the given <code>obj</code> to the <code>qualifiedName</code>. The filename is only
     * added to EObject that are directly under the root element of the Object-Tree. The filename is not added
     * to leaf elements again, because then the filename is added twice.
     */
    private QualifiedName addFileName(EObject obj, QualifiedName qualifiedName) {
        if (qualifiedName != null && !isChildElement(obj)) {
            Resource eResource = obj.eResource();
            URI uri = eResource.getURI();
            if (uri != null) {
                String lastSegment = uri.lastSegment();
                if (lastSegment != null) {
                    int i = lastSegment.lastIndexOf(".");
                    String name = lastSegment.substring(0, i);
                    qualifiedName = QualifiedName.create(name).append(qualifiedName);
                }
            }
        }
//        System.out.println("Return FQN: " + qualifiedName);
        return qualifiedName;
    }

    /**
     * Tests if the given EObject is Child Element. This is the case when
     * <code>obj.eContainer().eContainer()</code> is not <code>null</code>.
     */
    private boolean isChildElement(EObject obj) {
        EObject eContainer = obj.eContainer();
        if (eContainer == null)
            return false;
        EObject superEContainer = eContainer.eContainer();
        return superEContainer != null;
    }
}
